import XmlTkinter.Base as Base
import re

## represent RGB value model
class ColorModel(Base.BaseModel):
    def __init__(self):
        Base.BaseModel.__init__(self)
        self.r = 0
        self.g = 0
        self.b = 0
        self.locked = False
        self.ignore = False

    ## this is required to register the model for a concrete GUI element
    #  and all sub elements.
    def id(self):
        return "master"

    def autoCorrect(self, value):
        if value <   0: return   0
        if value > 255: return 255
        return value

    ## at least: notification by scale, entry or menu
    def updateValue(self, wid, value, mode):
        if self.ignore: return
        self.ignore = True

        if   wid == "color_r":
            diff   = int(value) - self.r
            self.r = int(value)
            self.setValue("edit_r", self.r)

            if self.locked:
                self.g = self.autoCorrect(self.g + diff)
                self.b = self.autoCorrect(self.b + diff)
                self.setValue("edit_g", self.g)
                self.setValue("edit_b", self.b)
                self.setValue("color_g", self.g)
                self.setValue("color_b", self.b)

        elif wid == "color_g":
            diff   = int(value) - self.g
            self.g = int(value)
            self.setValue("edit_g", self.g)

            if self.locked:
                self.r = self.autoCorrect(self.r + diff)
                self.b = self.autoCorrect(self.b + diff)
                self.setValue("edit_r" , self.r)
                self.setValue("edit_b" , self.b)
                self.setValue("color_r", self.r)
                self.setValue("color_b", self.b)

        elif wid == "color_b":
            diff   = int(value) - self.b
            self.b = int(value)
            self.setValue("edit_b", self.b)

            if self.locked:
                self.r = self.autoCorrect(self.r + diff)
                self.g = self.autoCorrect(self.g + diff)
                self.setValue("edit_r" , self.r)
                self.setValue("edit_g" , self.g)
                self.setValue("color_r", self.r)
                self.setValue("color_g", self.g)

        elif wid == "edit_r":
            if len(value) == 0: value = "0"
            self.r = int(value)
            self.setValue("color_r", self.r)
        elif wid == "edit_g":
            if len(value) == 0: value = 0
            self.g = int(value)
            self.setValue("color_g", self.g)
        elif wid == "edit_b":
            if len(value) == 0: value = 0
            self.b = int(value)
            self.setValue("color_b", self.b)

        elif wid == "make_equal_to_r":
            self.g = self.r
            self.b = self.r
            self.setValue("color_g", self.g)
            self.setValue("color_b", self.b)

        elif wid == "make_equal_to_g":
            self.r = self.g
            self.b = self.g
            self.setValue("color_r", self.r)
            self.setValue("color_b", self.b)

        elif wid == "make_equal_to_b":
            self.r = self.b
            self.g = self.b
            self.setValue("color_r", self.r)
            self.setValue("color_g", self.g)

        elif len(re.findall("#[0-9a-f]{6}", wid)) == 1:
            self.r = int(wid[1:3], 16)
            self.g = int(wid[3:5], 16)
            self.b = int(wid[5:7], 16)

            for shortcut in ["r", "g", "b"]:
                self.setValue("color_%c" % (shortcut), eval("self.%c" % (shortcut)))
                self.setValue("edit_%c"  % (shortcut), eval("self.%c" % (shortcut)))

        elif wid == "toggle_edit":
            print value
            newState = "disabled"
            if value == "1":
                newState = "normal"

            for wid in ["edit_r", "edit_g", "edit_b", "edit_rgb"]:
                self.setAttribute(wid, "state", newState)

            self.ignore = False
            return

        elif wid == "lock_unlock_sliders":
            self.locked = (value == "1")

        # updating canvas to display the color
        self.setAttribute("color", "background", "#%02x%02x%02x" % (self.r, self.g, self.b))
        self.setValue("edit_rgb", "#%02x%02x%02x" % (self.r, self.g, self.b))

        self.ignore = False

def createModel():
    return ColorModel()
